package com.lingxu.api.ztb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingxu.api.ztb.mapper.ProjectPreDesignMapper;
import com.lingxu.api.ztb.model.ProjectPreDesign;
import com.lingxu.api.ztb.model.vo.ProjectFeasibilityVO;
import com.lingxu.api.ztb.service.IProjectPreDesignService;
import com.lingxu.api.ztb.model.vo.ProjectPreDesignVO;



import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;


import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import javax.annotation.Resource;
import java.util.*;

/**
 * 初步设计表 服务实现类
 *
 * @author admin
 * @since 2025-08-14
 */
@Slf4j
@Service
public class ProjectPreDesignServiceImpl extends ServiceImpl<ProjectPreDesignMapper, ProjectPreDesign>  implements IProjectPreDesignService {

    @Resource
    private ProjectPreDesignMapper projectPreDesignMapper;

    @Transactional(readOnly = true)
    @Override
    public IPage<ProjectPreDesignVO> queryPageList(ProjectPreDesignVO query) {
        Page<ProjectPreDesignVO> page = new Page<ProjectPreDesignVO>(query.getPageNo(), query.getPageSize());
        return projectPreDesignMapper.pageList(page,query);
    }


    @Transactional(readOnly = true)
    @Override
    public List<ProjectPreDesignVO> list(ProjectPreDesignVO query) {
        return projectPreDesignMapper.selectProjectPreDesignList(query);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Set<String> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            projectPreDesignMapper.deleteBatchIds(ids);
        }
    }

    @Override
    public ProjectPreDesignVO queryPageInfo(ProjectPreDesignVO query) {
        List<ProjectPreDesignVO> list = projectPreDesignMapper.queryPageInfo(query.getProjectname());
        if (list == null || list.isEmpty()) {
            return new ProjectPreDesignVO();
        }
        ProjectPreDesignVO response = new ProjectPreDesignVO();
        ProjectPreDesignVO first = list.get(0);
        response.setProjectname(first.getProjectname());       // 项目名称
        response.setProjectCode(first.getProjectCode());       // 项目编号
        response.setApprovalDate(first.getApprovalDate());     // 申报批复时间

        Set<String> designDepartments = new HashSet<>();
        Set<String> designDeptHeads = new HashSet<>();
        Set<String> designUnits = new HashSet<>();
        Set<String> designHeads = new HashSet<>();
        Set<String> approvalUnits = new HashSet<>();

        for (ProjectPreDesignVO project : list) {
            addIfNotNull(designDepartments, project.getOwnerUnit());//申报单位
            addIfNotNull(designDeptHeads, project.getPreliminaryManager());//申报负责人
            addIfNotNull(designUnits, project.getDesignUnit());//设计单位
            addIfNotNull(designHeads, project.getDesignManager());//设计单位负责人
            addIfNotNull(approvalUnits, project.getApprovalUnit()); //审批单位
        }

        response.setOwnerUnit(joinSet(designDepartments));
        response.setPreliminaryManager(joinSet(designDeptHeads));
        response.setDesignUnit(joinSet(designUnits));
        response.setDesignManager(joinSet(designHeads));
        response.setApprovalUnit(joinSet(approvalUnits));

        return response;
    }
    private void addIfNotNull(Set<String> set, String value) {
        if (value != null && !value.trim().isEmpty()) {
            set.add(value.trim());
        }
    }

    private String joinSet(Set<String> set) {
        if (set == null || set.isEmpty()) {
            return "";
        }
        return String.join("/", set);
    }

}
